package org.go.listener.self;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import org.go.expcetion.SchedulerException;
import org.go.listener.SchedulerListener;
import org.go.trigger.Trigger;
import org.go.trigger.TriggerKey;

/**
 * Holds a List of references to SchedulerListener instances and broadcasts all
 * events to them (in order).
 *
 * <p>This may be more convenient than registering all of the listeners
 * directly with the Scheduler, and provides the flexibility of easily changing
 * which listeners get notified.</p>
 *
 * @see #addListener(org.quartz.SchedulerListener)
 * @see #removeListener(org.quartz.SchedulerListener)
 *
 * @author James House (jhouse AT revolition DOT net)
 */
public class BroadcastSchedulerListener implements SchedulerListener {

	private List<SchedulerListener> listeners;

	public BroadcastSchedulerListener() {
		listeners = new LinkedList();
	}

	/**
	 * Construct an instance with the given List of listeners.
	 *
	 * @param listeners the initial List of SchedulerListeners to broadcast to.
	 */
	public BroadcastSchedulerListener(List listeners) {
		this();
		this.listeners.addAll(listeners);
	}

	public void addListener(SchedulerListener listener) {
		listeners.add(listener);
	}

	public List<SchedulerListener> getListeners() {
		return java.util.Collections.unmodifiableList(listeners);
	}

	@Override
	public void jobScheduled(Trigger trigger) {
		Iterator itr = listeners.iterator();
		while (itr.hasNext()) {
			SchedulerListener l = (SchedulerListener) itr.next();
			l.jobScheduled(trigger);
		}
	}

	@Override
	public void jobsPaused(String jobGroup) {
		Iterator itr = listeners.iterator();
		while (itr.hasNext()) {
			SchedulerListener l = (SchedulerListener) itr.next();
			l.jobsPaused(jobGroup);
		}
	}

	@Override
	public void jobsResumed(String jobGroup) {
		Iterator itr = listeners.iterator();
		while (itr.hasNext()) {
			SchedulerListener l = (SchedulerListener) itr.next();
			l.jobsResumed(jobGroup);
		}
	}

	@Override
	public void jobUnscheduled(TriggerKey triggerKey) {
		Iterator itr = listeners.iterator();
		while (itr.hasNext()) {
			SchedulerListener l = (SchedulerListener) itr.next();
			l.jobUnscheduled(triggerKey);
		}
	}

	public boolean removeListener(SchedulerListener listener) {
		return listeners.remove(listener);
	}

	@Override
	public void schedulerError(String msg, SchedulerException cause) {
		Iterator itr = listeners.iterator();
		while (itr.hasNext()) {
			SchedulerListener l = (SchedulerListener) itr.next();
			l.schedulerError(msg, cause);
		}
	}

	@Override
	public void schedulerInited() {

	}

	@Override
	public void schedulerInStandbyMode() {
		Iterator itr = listeners.iterator();
		while (itr.hasNext()) {
			SchedulerListener l = (SchedulerListener) itr.next();
			l.schedulerInStandbyMode();
		}
	}

	@Override
	public void schedulerShutdown() {
		Iterator itr = listeners.iterator();
		while (itr.hasNext()) {
			SchedulerListener l = (SchedulerListener) itr.next();
			l.schedulerShutdown();
		}
	}

	@Override
	public void schedulerShuttingdown() {
		Iterator itr = listeners.iterator();
		while (itr.hasNext()) {
			SchedulerListener l = (SchedulerListener) itr.next();
			l.schedulerShuttingdown();
		}
	}

	@Override
	public void schedulerStarted() {
		Iterator itr = listeners.iterator();
		while (itr.hasNext()) {
			SchedulerListener l = (SchedulerListener) itr.next();
			l.schedulerStarted();
		}
	}

	@Override
	public void schedulingDataCleared() {
		Iterator itr = listeners.iterator();
		while (itr.hasNext()) {
			SchedulerListener l = (SchedulerListener) itr.next();
			l.schedulingDataCleared();
		}
	}

	@Override
	public void triggerFinalized(Trigger trigger) {
		Iterator itr = listeners.iterator();
		while (itr.hasNext()) {
			SchedulerListener l = (SchedulerListener) itr.next();
			l.triggerFinalized(trigger);
		}
	}

	@Override
	public void triggerPaused(TriggerKey key) {
		Iterator itr = listeners.iterator();
		while (itr.hasNext()) {
			SchedulerListener l = (SchedulerListener) itr.next();
			l.triggerPaused(key);
		}
	}

	@Override
	public void triggerResumed(TriggerKey key) {
		Iterator itr = listeners.iterator();
		while (itr.hasNext()) {
			SchedulerListener l = (SchedulerListener) itr.next();
			l.triggerResumed(key);
		}
	}

	@Override
	public void triggersPaused(String triggerGroup) {
		Iterator itr = listeners.iterator();
		while (itr.hasNext()) {
			SchedulerListener l = (SchedulerListener) itr.next();
			l.triggersPaused(triggerGroup);
		}
	}

	@Override
	public void triggersResumed(String triggerGroup) {
		Iterator itr = listeners.iterator();
		while (itr.hasNext()) {
			SchedulerListener l = (SchedulerListener) itr.next();
			l.triggersResumed(triggerGroup);
		}
	}
}
